<?php

class Zend_View_Helper_FormCaptcha extends Zend_View_Helper_FormElement
{
    public function formCaptcha($name, $value=null, $attribs=null)
    {
    	$info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable
    	
    	$idCaptcha = $this->getAttrib($attribs, Utils_Captcha::PARAM_ID, Utils_Captcha::PARAM_ID_DEFAULT);
    	$type = $this->getAttrib($attribs, Utils_Captcha::PARAM_TYPE, Utils_Captcha::PARAM_TYPE_DEFAULT);
    	$stringLength = $this->getAttrib($attribs, Utils_Captcha::PARAM_LENGTH, Utils_Captcha::PARAM_LENGTH_DEFAULT);
    	$sizeFont = $this->getAttrib($attribs, Utils_Captcha::PARAM_SIZE, Utils_Captcha::PARAM_SIZE_DEFAULT);
    	$message = $this->getAttrib($attribs, 'message', "captchaMessage");
    	$idImageCaptcha = 'idImageCaptcha';
    	
    	$src = $this->view->urlCaptcha($idCaptcha, $type, $stringLength, $sizeFont);
    	
    	$captcha = '';
    	$captcha .= $this->view->htmlImage($src, array('id' => $idImageCaptcha, 'class' => 'captchaImage', 'title' => $this->view->translate('form.label.captcha.code'), 'alt' => 'captcha'));
    	$captcha .= $this->view->htmlImage("/img/refresh2.png", array('width' => ($sizeFont*2).'px', 'height' => ($sizeFont*2).'px', 'onclick' => 'loadImage("'.$idImageCaptcha.'","'.$src.'"+new Date().getTime());', 'style' => "cursor: pointer;", 'alt' => $this->view->translate('form.label.captcha.refresh'), 'title' => $this->view->translate('form.label.captcha.refresh')));
		$captcha .= $this->view->formHidden(Utils_Captcha::PARAM_ID, $idCaptcha);
    	return $captcha;
    }
    
    private function getAttrib($attribs=null, $key=null, $default=null)
    {
    	if (is_array($attribs) && array_key_exists($key, $attribs)) {
    		return $attribs[$key];
    	}
    	else {
    		return $default;
    	}
    }
}
